//
//  MapLocationViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/23.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "MapLocationViewController.h"

@interface MapLocationViewController ()

@end

@implementation MapLocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = @"地图";
//    UIView *view1 = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    [self.view addSubview:view1];
    //初始化 地图 反地理编码
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"mapLocation.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction)];
    self.navigationItem.rightBarButtonItem = barButton;
    self.geocoder = [[CLGeocoder alloc] init];
    self.geocoder = [[CLGeocoder alloc] init];
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113)];
    self.mapView.delegate = self;
//    self.locationManager = [[CLLocationManager alloc] init];
    if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
        
        [self.locationManager requestWhenInUseAuthorization];
        
    }
    

    
    [self.view addSubview:self.mapView];
    
    //大头针显示
    CLLocationCoordinate2D location1=CLLocationCoordinate2DMake([self.nearbyDetail.lat floatValue], [self.nearbyDetail.lng floatValue]);
    KCAnnotation *kCAnnotation = [[KCAnnotation alloc] init];
    kCAnnotation.coordinate = location1;
    kCAnnotation.image=[UIImage imageNamed:@"icon_pin_floating.png"];
    [self.mapView addAnnotation:kCAnnotation];
    self.mapView.mapType = MKMapTypeStandard;

    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);			// 跨度（比例）
    MKCoordinateRegion region = MKCoordinateRegionMake(location1, span);       // 范围、区域

    [self.mapView setRegion:region];
    
    
    //反地理编码
    CLLocation *location=[[CLLocation alloc]initWithLatitude:[self.nearbyDetail.lat floatValue]longitude:[self.nearbyDetail.lng floatValue]];
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark=[placemarks firstObject];
        NSLog(@" ========  %@",placemark.addressDictionary);
    }];

    
    
    
    
    
    
}
-(void)buttonAction {

    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    self.mapView.showsUserLocation = YES;
    
    
}

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    
    [self.mapView removeFromSuperview];
    [self.view addSubview:mapView];
}

-(void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
    self.mapView.showsUserLocation = NO;
    self.mapView.delegate = nil;
    [self.mapView removeFromSuperview];
    self.mapView = nil;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
